<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ExcelExport\FlowExpoter;
use App\Admin\Extensions\Tools\ImportButton;
use App\Models\Flow;
use App\Models\Scene;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class FlowController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('客流');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新建');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Flow::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->exporter(new FlowExpoter());
            $grid->filter(function($filter){
                $filter->disableIdFilter();
                $filter->equal('time','时间')->select(config('options.flow_hour'));
                $filter->equal('scene_id','景区')->select(Scene::where('is_docking',1)->pluck('title','id'));
                $filter->equal('date','日期')->date();
            });
            $grid->id('ID')->sortable();
            $grid->tools(function ($tools) {
                $tools->append(new ImportButton('flows'));
            });
            $grid->scene_id('景区')->using(Scene::pluck('title','id')->toArray());
            $grid->date('日期')->sortable();
            $grid->time('时间')->display(function ($val){
                return config('options.flow_hour')[$val];
            });
            $grid->num('滞留人数');
            $grid->in('流入');
            $grid->out('流出');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Flow::class, function (Form $form) {
            $form->select('scene_id','景区选择')->options(Scene::where('is_docking',1)->pluck('title','id'))->rules('required');
            $form->date('date','日期')->format('YYYY-MM-DD')->rules('required');
            $form->radio('time','时间')->options(config('options.flow_hour'));
            $form->number('num','滞留人数');
            $form->number('in','流入');
            $form->number('out','流出');
            $form->rate('person_rate','散客占比')->default(null)->help('时间为当日时填写此项');
            $form->rate('online_rate','OTA占比')->default(null)->help('时间为当日时填写此项');
            //保存前回调
//            $form->saving(function (Form $form) {
//                if($form->person_rate==null||$form->online_rate==null&&$form->time==0){
//                    $error = new MessageBag([
//                        'title'   => 'title...',
//                        'message' => '请填写占比',
//                    ]);
//                    return back()->with(compact('error'));
//                }
//            });
        });
    }
}
